package vn.com.vega.music.view.adapter;

import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.Appinstant;
import vn.com.vega.music.objects.NewsEntry;
import vn.com.vega.music.utils.Const;
import vn.com.vega.music.view.adapter.base.AutoLoadingAdapter.AutoLoadingInterface;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NewsListAdapter extends BaseAdapter implements Const,
		AutoLoadingInterface {
	private Activity mContext;
	// private ImageLoader mImageLoader;
	private Appinstant mApp;
	private LayoutInflater mInflater;
	private List<Object> mListnews = new ArrayList<Object>();

	public NewsListAdapter(Activity context, List<Object> objects) {
		mContext = context;
		// mImageLoader = ImageLoader.getInstance(context);
		mApp = (Appinstant) context.getApplication();
		mInflater = LayoutInflater.from(context);
		if (objects != null) {
			mListnews = objects;
		}
	}

	@Override
	public int getCount() {
		return mListnews.size();
	}

	@Override
	public Object getItem(int position) {
		return mListnews.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		Object obj = mListnews.get(position);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.news_item, null);
			holder = new ViewHolder();
			holder.thumb = (ImageView) convertView
					.findViewById(R.id.imv_news_thumb);
			holder.title = (TextView) convertView
					.findViewById(R.id.tv_news_title);
			holder.time = (TextView) convertView
					.findViewById(R.id.tv_news_time);
			holder.des = (TextView) convertView.findViewById(R.id.tv_news_des);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		NewsEntry news = (NewsEntry) obj;
		holder.title.setText(news.title);
		holder.time.setText(news.datetime);
		holder.des.setText(news.content);
		holder.thumb.setVisibility(View.VISIBLE);
		mApp.mImageLoader.download(news.thumbUrl, holder.thumb);

		return convertView;
	}

	static class ViewHolder {
		ImageView thumb;
		TextView title;
		TextView time;
		TextView des;
	}

	@Override
	public void notifyListObjectChanged(List<Object> objectsChanged) {
		notifyListObjectChanged(objectsChanged, TYPE_LIST_ADDED);
	}

	@Override
	public void notifyListObjectChanged(List<Object> objectsChanged, int type) {
		if (objectsChanged != null) {
			if (type == TYPE_LIST_REPLACED) {
				mListnews.clear();
			}
			mListnews.addAll(objectsChanged);
			notifyDataSetChanged();
		}
	}

	public List<Object> getListObject() {
		// TODO Auto-generated method stub
		return mListnews;
	}
}
